<?php
/**
 * Created by PhpStorm.
 * User: ytlmi
 * Date: 2017/12/24
 * Time: 15:54
 */

namespace app\sample_rsv\controller;
use app\common\controller\BaseController;
use app\sample_rsv\model\Sample;
use app\sample_rsv\model\Unit;
use app\sample_rsv\model\SampleType as Type;
use think\exception\PDOException;

class Statistics extends BaseController
{
    protected $partName='统计';
    protected $preData=null;
    protected $model='app\sample_rsv\model\Sample';

    /*
     * option格式：
     * ‘year'=>'2017'
     * 'id_unit'=>2
     * 'id_type'=>3*/
    public function getByYear()
    {
        $option=input();
        $yearNow=date('Y');
        $monthNow=date('m');
        $arr_data=[];
        ($option['year']==$yearNow && $monthNow!='12') ? $maxMonth=$monthNow+1 : $maxMonth=12 ;
        for ($i=1;$i<=$maxMonth;$i++){
            $preData=Sample::view('sample','id,sample_name,id_type,id_group,person_name,person_race,person_sex,create_time')->view('group','id_unit','sample.id_group=group.id','LEFT');
            $this_option=$option;
            unset($this_option['year']);
            $this_option['date_from']=$option['year'].'-'.$i;
            if($i!=12){
                $this_option['date_to']=$option['year'].'-'.($i+1);
            }else{
                $this_option['date_to']=($option['year']+1).'-1';
            }
            $arr_data[$i-1]['month']=$i.'月';
            $arr_data[$i-1]['count']=$this->getPreData($preData,$this_option)->count();
        }

        return $arr_data;
    }

    public function getByUnit()
    {
        $option=input();
        $arr_unit=Unit::column('id,name');
        $arr_id=array_keys($arr_unit);
        $arr_name=array_values($arr_unit);
        $arr_data=[];
        for ($i=0;$i<count($arr_unit);$i++){
            $preData=Sample::view('sample','id,sample_name,id_type,id_group,person_name,person_race,person_sex,create_time')->view('group','id_unit','sample.id_group=group.id','LEFT');
            $this_option=$option;
            unset($this_option['year']);
            $this_option['date_from']=$option['year'].'-1-1';
            $this_option['date_to']=$option['year'].'-12-31';
            $this_option['id_unit']=$arr_id[$i];
            $arr_data[$i]['name']=$arr_name[$i];
            $arr_data[$i]['count']=$this->getPreData($preData,$this_option)->count();
        }
//        var_dump($this_option);
        return $arr_data;
    }

    public function getByType()
    {
        $option=input();
        $arr_unit=Type::column('id,name');
        $arr_id=array_keys($arr_unit);
        $arr_name=array_values($arr_unit);
        $arr_data=[];
        for ($i=0;$i<count($arr_unit);$i++){
            $preData=Sample::view('sample','id,sample_name,id_type,id_group,person_name,person_race,person_sex,create_time')->view('group','id_unit','sample.id_group=group.id','LEFT');
            $this_option=$option;
            unset($this_option['year']);
            $this_option['date_from']=$option['year'].'-1-1';
            $this_option['date_to']=$option['year'].'-12-31';
            $this_option['id_type']=$arr_id[$i];
            $arr_data[$i]['name']=$arr_name[$i];
            $arr_data[$i]['count']=$this->getPreData($preData,$this_option)->count();
        }
//        var_dump($this_option);
        return $arr_data;
    }

    public function test()
    {
//        $yearNow=date('Y');
//        $monthNow=date('m');
//        var_dump($yearNow);
    }

}